Skip to content

Ruff cleanup of remap_restart, bug fixes, and v14 as default BCs - #280

Open
mathomp4 wants to merge 37 commits into
mainfrom
feature/ruff-fix-remap
Open

mathomp4 wants to merge 37 commits into
mainfrom
feature/ruff-fix-remap

Conversation

@mathomp4

@mathomp4 mathomp4 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Cleanup of the Python code in pre/remap_restart driven by ruff and ty, plus a few bug fixes found along the way, plus making v14 the default BCs version in the interactive questions.

Testing: zero-diff. Remaps were run on Discover from this branch and the results were identical to main.

Code cleanup (ruff)

Ruff went from 116 findings to 0.

  • Safe autofixes: unused imports, import sorting, f-strings, setattr -> attribute assignment.
  • Removed unused variables and imports.
  • exit() -> sys.exit().
  • File handling uses context managers (the remap_bin2nc yaml handle was never closed).
  • Replaced from module import * everywhere with explicit imports, so linters and type checkers can now see undefined names.
  • Misc style cleanups (nested ifs, direct returns, .keys()/dict() removal).
  • Added pre/remap_restart/ruff.toml.
  • Python ignores (__pycache__/, *.py[cod], .ruff_cache/, .pytest_cache/, .venv/) moved to the root .gitignore.

Behavior changes

  • Subprocess failures now raise. subprocess.call -> subprocess.run(..., check=True) for the work steps (dyn2dyn.x, cp, chmod, qsub/sbatch). os.system(script > log 2>&1) is replaced by subprocess.run with the log redirected the same way. A failed step now stops the run with a CalledProcessError instead of continuing silently. rm -rf cleanups and the diff check in remap_base.py are unchanged.
  • Scratch directories removed after a successful remap: InData/, OutData/, upper_data/, ana_data/. On failure they are left for debugging.
  • remap_params.yaml is written without line wrapping, which fixes a trailing space after bc_base: (ruamel.yaml wraps at 80 columns).

Bugs fixed

  • shutil.remove does not exist; now os.remove (3 places in remap_other_restarts.py). It would raise AttributeError on a rerun into an existing directory.
  • 'Ganymed' in bc_version raised TypeError if bc_version was missing; the lookup now defaults to ''.
  • get_resolutions crashed on None if agrid or ogrid was missing; it now raises a clear ValueError. All callers seen pass both.
  • ty flagged these; ruff could not, since it doesn't check module attributes.

New default BCs version: v14

In the interactive questions, v14 is now the default and is listed in the main menu. v12 stays in the main menu. v12 and v14 are removed from the "Other" submenu so they aren't listed twice. The command-line -bcvin/-bcvout choices are unchanged.

Main menu table, before:

 BCs version      | ADAS tags            | GCM tags typically used with BCs version
 -----------------|----------------------|-----------------------------------------
 v12: v12         | future               | 12.0             ... present
 NL3: Icarus-NLv3 | 5_25_1 ... present   | Icarus_NL, 10.19 ... 11.7
 ICA: Icarus      | 5_17_0 ... 5_24_0_p1 | Icarus, Jason    ... 10.18
 ----------------------------------------------------------------------------------
 Other: Additional choices used in model or DAS development.

Choices: v12 (default), NL3, ICA, Other. "Other" offered v11, v12, v13, v14, v15_BETA.

Main menu table, after:

 BCs version      | ADAS tags            | GCM tags typically used with BCs version
 -----------------|----------------------|-----------------------------------------
 v14: v14         | future               | 11.11.0          ... present
 v12: v12         | future               | 11.10.0          ... 11.10.x
 NL3: Icarus-NLv3 | 5_25_1 ... present   | Icarus_NL, 10.19 ... 11.9
 ICA: Icarus      | 5_17_0 ... 5_24_0_p1 | Icarus, Jason    ... 10.18
 ----------------------------------------------------------------------------------
 Other: Additional choices used in model or DAS development.

Choices: v14 (default), v12, NL3, ICA, Other. "Other" now offers v11, v13, v15_BETA.

Notes for reviewers: v12 became the default in GEOSgcm v11.10.0 and v14 in v11.11.0, hence the new GCM ranges. The ADAS column for v14 and v12 is still "future". The v12 end tag (11.10.x) is a placeholder, so please check it.

CI

New workflow .github/workflows/python-lint.yml runs on PRs touching pre/remap_restart:

  • ruff (blocking)
  • ty (blocking). It is clean after the get_resolutions fix below.

Not changed

  • Tests still need NCCS baseline data and were not run in CI.
  • ruff format / indentation normalisation (the code mixes 2/3/5-space indents) is left for a separate PR.

Remove unused imports, sort imports, replace constant setattr with
attribute assignment, convert to f-strings, and other safe fixes.
Drop dead assignments (dd_, rst_time, time, ymdh, etc.), discard unused
communicate() stderr results, and keep p.wait() calls without binding
their return codes.
exit() is only guaranteed by the site module. Behavior is unchanged:
exit(msg) and sys.exit(msg) both print msg to stderr and exit with 1.
Replace manual open()/close() with with-blocks. In remap_analysis, read the
source file directly instead of via the global fileinput singleton, which
was never closed. remap_bin2nc previously never closed its yaml handle.
Convert percent formatting to f-strings, merge nested ifs, return
conditions directly, drop redundant .keys() and dict(), and merge
isinstance calls. No behavior change.
@mathomp4 mathomp4 added the 0 diff The changes in this pull request have verified to be zero-diff with the target branch. label Oct 15, 2026
@gmao-rreichle

Copy link
Copy Markdown
Contributor

@mathomp4 : I added info about v14 into "message_bc_other". This is just a message that's printed, although it may be confusing that it includes "v14" documentation when "v14" is in the "ops" menu and not in the "other" menu. Perhaps we should also add similar info about the old "ops" bcs ("NL3" etc) in "message_bc_other"? That'd require some thought because a shorthand description for the old "ops" bcs does not yet exist. Let me know if you I should give this a try.

FYI, the descriptions are from make_bcs, which is in a different repo
https://github.com/GEOS-ESM/GEOSgcm_GridComp/blob/23e58ba496c1968030ad656d895bcde7fe0d7edf/GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/Raster/makebcs/make_bcs_questionary.py#L199

@gmao-rreichle

Copy link
Copy Markdown
Contributor

PS: To answer some of your questions:

The v12 description is correct.

Like I said, the original intent was to keep stuff like v14 in "other". I wouldn't be opposed to moving it there, but I don't insist.

I'm not sure if the "testing only" entries are 100% accurate. I don't think these bcs were ever made the default, so probably yes. But I didn't verify. Having said that, I don't think this matters much.

In last commit, I reduced the version info to major/minor and dropped the bug fix (third) digit for simplicity.

Finally, I don't think we want to provide technical/science descriptions with the "ops" bcs choices. If we include some detail, I would include it in "message_bc_other" for reference.

I hope I'm not forgetting anything

@gmao-rreichle

Copy link
Copy Markdown
Contributor

@mathomp4 : Does your "thumbs up" emoji in response to my previous comment mean that I should take a crack at adding brief descriptions of old "ops" bcs into "message_bc_other"? Please let me know, thanks

@mathomp4

Copy link
Copy Markdown
Member Author

@mathomp4 : Does your "thumbs up" emoji in response to my previous comment mean that I should take a crack at adding brief descriptions of old "ops" bcs into "message_bc_other"? Please let me know, thanks

Oh. I thought you did that in 92db16b

Did you mean you might have more you'd like to add? If so, please do!

@gmao-rreichle

Copy link
Copy Markdown
Contributor

@mathomp4 , @biljanaorescanin :

  • For completeness, I added brief descriptions of ICA and NL3 ("ops") bcs into the message shown when users select "other" bcs. I also re-ordered the "other" list of bcs. See dec2758 (apologies for the garbled commit message, my browser was acting up).
  • @biljanaorescanin : I'm wondering why we have GM4 in the options for command line remapping but not in the drawdown menu. Do you remember what the difference was between ICA and GM4? Should GM4 be in the drawdown list?

@gmao-rreichle

Copy link
Copy Markdown
Contributor

Re. my previous commit: We could conceivably repeat the "ops" bc choices within the "other" bc choices list. After my commit, users only get to select the "ops" choices in the "ops" list, but they see descriptions of the "ops" bcs in the Table shown with the "other" choices. Thoughts?

@mathomp4

Copy link
Copy Markdown
Member Author

Re. my previous commit: We could conceivably repeat the "ops" bc choices within the "other" bc choices list. After my commit, users only get to select the "ops" choices in the "ops" list, but they see descriptions of the "ops" bcs in the Table shown with the "other" choices. Thoughts?

@gmao-rreichle as it is currently when you run interactively you'll first see:

? Select boundary conditions (BCs) version of input restarts:


 BCs version      | ADAS tags            | GCM tags typically used with BCs version
 -----------------|----------------------|-----------------------------------------
 v14: v14         | future               | 11.11            ... present
 NL3: Icarus-NLv3 | 5_25_1 ... present   | Icarus_NL, 10.19 ... 11.9
 ICA: Icarus      | 5_17_0 ... 5_24_0_p1 | Icarus, Jason    ... 10.18
 ----------------------------------------------------------------------------------
 Other: Additional choices used in model or DAS development.

 (Use arrow keys)
 » v14
   NL3
   ICA
   Other

if you choose other then:

? Select boundary conditions (BCs) version of input restarts:


 BCs version      | ADAS tags            | GCM tags typically used with BCs version
 -----------------|----------------------|-----------------------------------------
 v14: v14         | future               | 11.11            ... present
 NL3: Icarus-NLv3 | 5_25_1 ... present   | Icarus_NL, 10.19 ... 11.9
 ICA: Icarus      | 5_17_0 ... 5_24_0_p1 | Icarus, Jason    ... 10.18
 ----------------------------------------------------------------------------------
 Other: Additional choices used in model or DAS development.

 Other
? Select BCs version of input restarts:


 BCs version | GCM tags                   | Description
 ------------|----------------------------|------------------------------------------------------------
 ICA         | Icarus, Jason    ... 10.18 | See Mahanama et al. 2015 GMAO Tech Memo Vol. 39
 NL3         | Icarus_NL, 10.19 ... 11.9  | See Mahanama et al. 2015 GMAO Tech Memo Vol. 39
 v11         | testing only               | NL3 + JPL veg height + PEATMAP + MODIS snow alb v2
 v12         | 11.10                      | NL3 + JPL veg height + PEATMAP + MODIS snow alb v2 + Argentina peatland fix
 v13         | testing only               | As in v12 + mean land elevation fix + MOM6 v2 (OM4) bathymetry
 v14         | 11.11 ...                  | As in v13 but with GPM 2.0 peat
 v15_BETA    | testing only               | As in v14 but with v2 topography for atmosphere

 (Use arrow keys)
 » v15_BETA
   v13
   v12
   v11

It's definitely completek though I guess a bit odd that you can't select some in the second table.

@biljanaorescanin what are your thoughts?

@biljanaorescanin

Copy link
Copy Markdown
Contributor
  • @biljanaorescanin : I'm wondering why we have GM4 in the options for command line remapping but not in the drawdown menu. Do you remember what the difference was between ICA and GM4? Should GM4 be in the drawdown list?

My understanding is that GM4 (Ganymed-4_0) is retained mainly for remapping archived MERRA-2 restarts, since MERRA-2 uses GM4 BCs. This is handled explicitly in init_merra2(), where the input BC version is set to GM4.

GM4 predates ICA, and they are different BC generations (mostly a lot of bug fixes, mask changes and formating changes I have listed in notes). I don't think we need GM4 in the interactive dropdown for output BCs, since we generally remap from GM4 to newer versions rather than to GM4.

I would keep GM4 supported for command-line remapping and the MERRA-2 input workflow, but wouldn't add it as an output choice unless there is a specific use case for generating GM4 compatible restarts.

gmao-rreichle and others added 2 commits October 16, 2026 13:13
…l"; removed separate explicit list of BCs for command line (remap_utils.py, remap_questions.py, remap_command_line.py, remap_catchANDcn.py)
@gmao-rreichle

Copy link
Copy Markdown
Contributor

@mathomp4 , @biljanaorescanin :

My last two commits are an attempt to standardize and clean up. Specifically:

  • The first ("short") list offers only the bcs that have been used in ops product and the v14 bcs used in ongoing model development.
  • The second ("long") list offers all supported bcs choices.
  • The "long" bcs list is also used for the command line option.
  • I added GM4 to the 2nd list in the drawdown menu for completeness. Biljana: This list is also used for the input bcs, and it's still very much common that users want to remap from MERRA-2.

Alas, my commit fails some "ruff" checks. The first failure is because a list of variables is not sorted alphabetically. The list is ordered meaningfully, and it should not be resorted alphabetically. Not sure how to get ruff to relent.

Please take a look and let me know if you agree with the changes. If so, Biljana can restart the testing.

@mathomp4

Copy link
Copy Markdown
Member Author

@mathomp4 , @biljanaorescanin :

Alas, my commit fails some "ruff" checks. The first failure is because a list of variables is not sorted alphabetically. The list is ordered meaningfully, and it should not be resorted alphabetically. Not sure how to get ruff to relent.

@gmao-rreichle What exactly do you mean? Python has no notion of imports having an order. Imports are just imports and you can technically specify them in any order. That isn't a "real" Python tuple or list, just syntactic sugar to allow us to not do a from foo import *.

But if you want, I can tell ruff to not care about order in that import list.

@gmao-rreichle

Copy link
Copy Markdown
Contributor

@mathomp4 : Here's are the linter errors to which I was referring:

https://github.com/GEOS-ESM/GEOS_Util/actions/runs/38940178729/job/117105533406?pr=280

I didn't look too closely, but it sounded like "ruff" was complaining about an "un-sorted" import block.

Perhaps it's on us to just ignore the errors and merge anyway. I just noticed that there's a failed check in the list of CI tests:

image

@mathomp4

Copy link
Copy Markdown
Member Author

@mathomp4 : Here's are the linter errors to which I was referring:

Yes, I know. We are "violating" the style that ruff (and other tools prefer). But if you prefer non-sorted imports, I can tell it to ignore. I'll make a push.

@gmao-rreichle

Copy link
Copy Markdown
Contributor

One more question for @biljanaorescanin : What is the status of "v15_BETA"? I don't see it in the bcs_shared dir (although I admittedly didn't look very hard). Also, since v14 still uses the old (v1) atmos topography, does this mean that the v12 GCM (and thus MERRA-3) won't be using the new topography????? Do we need to remove "v15_BETA" from the list if it's not in the bcs_shared dir?

@biljanaorescanin

Copy link
Copy Markdown
Contributor

One more question for @biljanaorescanin : What is the status of "v15_BETA"? I don't see it in the bcs_shared dir (although I admittedly didn't look very hard). Also, since v14 still uses the old (v1) atmos topography, does this mean that the v12 GCM (and thus MERRA-3) won't be using the new topography????? Do we need to remove "v15_BETA" from the list if it's not in the bcs_shared dir?

Status is we need to have stable GCMv12 version so I can work with Nathan on tuning. Just 2 weeks ago we moved to v14 BCs... before this we were on v12...

so we have :
bcs_shared/make_bcs_inputs/atmosphere/TOPO/v1 current v14
bcs_shared/make_bcs_inputs/atmosphere/TOPO/v2 will be used in v15_beta

@gmao-rreichle

Copy link
Copy Markdown
Contributor

One more question for @biljanaorescanin : What is the status of "v15_BETA"? I don't see it in the bcs_shared dir (although I admittedly didn't look very hard). Also, since v14 still uses the old (v1) atmos topography, does this mean that the v12 GCM (and thus MERRA-3) won't be using the new topography????? Do we need to remove "v15_BETA" from the list if it's not in the bcs_shared dir?

Status is we need to have stable GCMv12 version so I can work with Nathan on tuning. Just 2 weeks ago we moved to v14 BCs... before this we were on v12...

so we have : bcs_shared/make_bcs_inputs/atmosphere/TOPO/v1 current v14 bcs_shared/make_bcs_inputs/atmosphere/TOPO/v2 will be used in v15_beta

So you're saying that v15_BETA does not even exist at this time, right? In that case, we need to remove it from the drawdown list! Please confirm.

Looking ahead, this also means that (i) we may not really want to have "v14" in the "ops" list of bcs, if it's going to be superseded in short order, and (ii) I might be able to shore up my retirement funds by putting some money on the v12 GCM not being ready by the end of the year. The problem with the latter, of course, is that I don't see who would want to be betting against me ;)

@gmao-rreichle gmao-rreichle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@biljanaorescanin , see comments below

Comment thread pre/remap_restart/remap_questions.py Outdated
"message": message_bc_all_new,
"choices": choices_bc_all,
"when": lambda x: x["output:shared:bc_version"] == 'Other' and x["input:shared:bc_version"] not in ['v11', 'v12', 'v13', 'v14', 'v15_BETA'],
"when": lambda x: x["output:shared:bc_version"] == 'Other' and x["input:shared:bc_version"] not in ['v11', 'v12', 'v13', 'v14'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@biljanaorescanin : I'm trying to understand what exactly the "when:" field does here. What's confusing me is that the list of "not in" bcs doesn't line up with what in the "ops" vs. "all" lists. For starters, we probably need to replace 'Other' with 'All', which I probably forgot to do when I made the corresponding naming change last week. But do we also have to remove "v14" from the list here because it's in the "ops" bcs list? And, in any case, can we avoid the hard-coded list of bcs here?

@biljanaorescanin biljanaorescanin Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the Otherto All rename needs to be carried through to the when conditions. I found three remaining checks in remap_questions.py and one in remap_catchANDcn.py, so we should update the input and land-only paths as well.

The when field determines whether Questionary shows or skips a question based on the answers collected so far. Here, the two expanded output questions offer the same BC choices, the difference is whether they display the full description table or just the short prompt. The hard coded input-version lists appear intended to avoid repeating that table, rather than restrict which BCs can be selected.
Since the short and complete lists now overlap, the input BC version alone no longer tells us whether the user has already seen the complete table. For example, v14 can be selected directly from the short menu or through All.
Rather than adjust the conditions by removing v14, I suggest replacing those two expanded output questions with a single question using message_bc_all_new and choices_bc_all, shown whenever the output selection is All. That would keep the short menu unchanged and remove the hard coded BC-version lists from those conditions.
The tradeoff is that users selecting All for both input and output would see the detailed table twice.
Would that approach work for you?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks

Comment thread pre/remap_restart/remap_questions.py Resolved
@biljanaorescanin

Copy link
Copy Markdown
Contributor

The tests are not zero-diff because they have not been rebased after the ESMF update.

c24Toc12: Succeeded
f522Toc360: Failed
amip_c180Toc90: Succeeded
c180Toc360: Failed
c360Toc24: Succeeded

Both f522Toc360 and c180Toc360 fail in the fvcore RST, but for different variables:

  • f522Toc360: PT
  • c180Toc360: DZ

If we use the old build with the old ESMF, all tests are zero-diff.

=> We need to rebase.

So I think we can proceed with rebasing and merging this PR. After that, I will work on the testing-package issue Matt created:
#282

That issue will probably require some additional discussion about what we want from the testing package. For example, we may also want to revisit which BCs are used in testing and whether additional tests are needed. However, I think that work should be handled independently of this PR.

@biljanaorescanin

Copy link
Copy Markdown
Contributor

@sdrabenh its been two weeks we still didn't merge this...?
Once merged @mathomp4 needs to rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0 diff The changes in this pull request have verified to be zero-diff with the target branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants